package com.gklyj.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
        String uri = request.getRequestURI();
        Object loginUser = request.getSession().getAttribute("username");
        System.out.println(loginUser);
//        if(uri.startsWith("/main") && null == loginUser){
//            response.sendRedirect("/tologin");
//            return false;
//        }
//        return true;
//9.0.52
        if (uri.contains("login") || uri.contains("tologin")){
            return true;
        }else {
            if (loginUser != null){
                return true;
            }else {
                response.sendRedirect(request.getContextPath() + "/tologin");
            }
        }
        return false;
    }
}
